package com.whty.module.service;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


import com.whty.module.mapper.DepotHeadMapper;
import com.whty.system.domain.DepotHeadItem;
import com.whty.system.mapper.DepotHeadItemMapper;
import com.whty.system.service.IDepotHeadItemService;
import com.whty.module.domain.DepotHead;

import com.whty.utils.ReadExcel;
import com.whty.common.support.Convert;
import com.whty.mariotools.IdGenerator;
import com.whty.mariotools.excle.ExcelImport;

/**
 * 入库记录 服务层实现
 * 
 * @author mario
 * @date 2018-10-31
 */
@Service("depotHead")
public class DepotHeadServiceImpl implements IDepotHeadService 
{
	@Autowired
	private DepotHeadMapper depotHeadMapper;
	@Autowired
	private DepotHeadItemMapper depotHeadItemMapper;
	
	@Autowired
	private IDepotHeadItemService depotHeadItemService;
	  final static Logger logger = LoggerFactory.getLogger(DepotHeadServiceImpl.class);

	/**
     * 查询入库记录信息
     * 
     * @param id 入库记录ID
     * @return 入库记录信息
     */
    @Override
	public DepotHead selectDepotHeadById(Integer id)
	{
	    return depotHeadMapper.selectDepotHeadById(id);
	}
	
	/**
     * 查询入库记录列表
     * 
     * @param depotHead 入库记录信息
     * @return 入库记录集合
     */
	@Override
	public List<DepotHead> selectDepotHeadList(DepotHead depotHead)
	{
	    return depotHeadMapper.selectDepotHeadList(depotHead);
	}
	
    /**
     * 新增入库记录
     * 
     * @param depotHead 入库记录信息
     * @return 结果
     */
	@Override
	public int insertDepotHead(DepotHead depotHead)
	{
	    return depotHeadMapper.insertDepotHead(depotHead);
	}
	
	/**
     * 修改入库记录
     * 
     * @param depotHead 入库记录信息
     * @return 结果
     */
	@Override
	public int updateDepotHead(DepotHead depotHead)
	{
	    return depotHeadMapper.updateDepotHead(depotHead);
	}

	/**
     * 删除入库记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteDepotHeadByIds(String ids)
	{
		return depotHeadMapper.deleteDepotHeadByIds(Convert.toStrArray(ids));
	}

	/**
	 * 批量导入
	 */
	@Override
	@Transactional
	public String batchImport(MultipartFile file, String tagType,int amount,String remarks,String fileName) {
		
	    try{
	    	 ExcelImport ei = new ExcelImport(file, 1, 0);
			 List<DepotHeadItem> list = ei.getDataList(DepotHeadItem.class);
			 
			 if(list!=null&&list.size()>0){
				 //判断uid是否已经导入			
				 if(list.size()!=amount){
					 return "导入数据条数与实际Excle数据数量不一致"; 
				 }
				 
				 //1.校验导入数据长度 、uid和序列号是否重复
				 for(DepotHeadItem depotHeadItem:list){
					 //uid 、标签序列号均不能重复
					
					 if(depotHeadItem.getUid()!=null&&depotHeadItem.getUid().length()!=14){

						 return "导入的数据uid："+depotHeadItem.getUid()+"长度不正确，应为14位";  
					 }
					 if(depotHeadItem.getLabelSerialNumber()!=null&&depotHeadItem.getLabelSerialNumber().length()!=14){
						 return "导入的数据签序列号："+depotHeadItem.getLabelSerialNumber()+"长度不正确，应为14位"; 
					 }
					 DepotHeadItem tempDepotHeadItem=new DepotHeadItem();
					 tempDepotHeadItem.setUid(depotHeadItem.getUid());
					 List<DepotHeadItem> listTemp1 = depotHeadItemService.selectDepotHeadItemList(tempDepotHeadItem);
					 if(listTemp1!=null&&listTemp1.size()>0){
						 return "导入的数据uid："+depotHeadItem.getUid()+"重复"; 
					 }
					 DepotHeadItem tempDepotHeadItem1=new DepotHeadItem();
					 tempDepotHeadItem1.setLabelSerialNumber(depotHeadItem.getLabelSerialNumber());
					 List<DepotHeadItem> listTemp2 = depotHeadItemService.selectDepotHeadItemList(tempDepotHeadItem1);
					 if(listTemp2!=null&&listTemp2.size()>0){
						 return "导入的数据标签序列号："+depotHeadItem.getLabelSerialNumber()+"重复"; 
					 }
					 
					
				 }
				 
				 String depotHeadNo=IdGenerator.getRanId18();
				 //2.再插入明细
				 for(DepotHeadItem depotHeadItem:list){
					 depotHeadItem.setDepotHeadNo(depotHeadNo);
					 depotHeadItem.setReleaseStatus("01");
					 depotHeadItem.setAllocationStatus("00");
					 depotHeadItem.setUpdateTime(new Date());
					 				 
					 depotHeadItemService.insertDepotHeadItem(depotHeadItem);
					 
				 }
				 //1.先插入抬头
				 DepotHead depotHead =new DepotHead();
				 depotHead.setAmount(amount);
				 depotHead.setRemark(remarks);
				 depotHead.setBeginSerialNumber(list.get(0).getLabelSerialNumber());
				 depotHead.setEndSerialNumber(list.get(amount-1).getLabelSerialNumber());
				
				 depotHead.setDepotHeadNo(depotHeadNo);
				 depotHead.setCreateTime(new Date());
				 depotHead.setItemFileName(fileName);
				 depotHead.setTagType(tagType);
				 depotHead.setStatus("00");
//				 depotHead.setCurrentSerialNumber("0");
				 depotHead.setUsedAmount(0);
				 depotHead.setUnusedAmount(amount);
				 depotHead.setUpdateTime(new Date());
				 insertDepotHead(depotHead);
				 
			 }else{
				 return "解析的数据条数为0"; 
				 
			 }
	    }catch(Exception e){
	    	logger.info("导入数据异常",e);
	    	return "导入数据异常"; 
	    }
		

	     return "00";
	}

	/**
	 * 只查询没有使用完的入库单
	 */
	@Override
	public List<DepotHead> getDepotHead() {
		// TODO Auto-generated method stub
		return depotHeadMapper.selectDepotHeadListByStatus();
	}

	


	
}
